import VueRouter from 'vue-router'

import Login from '../pages/Login.vue'
import Home from '../pages/Home.vue'
import StudentQuery from '../pages/student/StudentQuery.vue'
import TeacherQuery from '../pages/teacher/TeacherQuery.vue'
import CourseQuery from '../pages/course/CourseQuery.vue'
import AddStudent from '../pages/student/AddStudent.vue'
import AddTeacher from '../pages/teacher/AddTeacher.vue'
import AddCourse from '../pages/course/AddCourse.vue'
import UpdateCourse from '../pages/course/UpdateCourse.vue'
import UpdateStudent from '../pages/student/UpdateStudent.vue'
import UpdateTeacher from '../pages/teacher/UpdateTeacher.vue'
import QuerySC from '../pages/student/QueryStudentCourse.vue'
import Appointment from '../pages/teacher/Appointment.vue'
import AddAdmin from '../pages/AddAdmin.vue'

const router = new VueRouter({
    routes:[
        {
            path:"/",
            redirect:"/login",
        },
        {
            name:"login",
            path:"/login",
            component:Login,
        },
        {
            path:"/home",
            component:Home,
            meta:{isAuth:true},
            children:[
                {
                    name:"addAdmin",
                    path:"addAdmin",
                    component:AddAdmin,
                    meta:{isAuth:true},
                },
                {
                    path:"student/query",
                    component:StudentQuery,
                    meta:{isAuth:true},
                    children:[
                        {
                            name:"updateStudent",
                            path:"updateStudent/:student_id",
                            component:UpdateStudent,
                            meta:{isAuth:true},
                        },
                        {
                            name:"addStudent",
                            path:"addStudent",
                            component:AddStudent,
                            meta:{isAuth:true},
                        },
                    ]
                },
                
                {
                    path:"student/querySC",
                    component:QuerySC,
                    meta:{isAuth:true},
                },
                {
                    path:"teacher/query",
                    component:TeacherQuery,
                    meta:{isAuth:true},
                    children:[
                        {
                            name:"updateTeacher",
                            path:"updateTeacher/:teacher_id",
                            component:UpdateTeacher,
                            meta:{isAuth:true},
                        },
                        {
                            name:"addTeacher",
                            path:"addTeacher",
                            component:AddTeacher,
                            meta:{isAuth:true},
                        }
                    ]
                },
                
                {
                    path:"teacher/appointment",
                    component:Appointment,
                    meta:{isAuth:true},
                },
                {
                    path:"course/query",
                    component:CourseQuery,
                    meta:{isAuth:true},
                    children:[
                        {
                            name:"updateCourse",
                            path:"updateCourse/:course_id",
                            component:UpdateCourse,
                            meta:{isAuth:true},
                        },
                        {
                            name:"addCourse",
                            path:"addCourse",
                            component:AddCourse,
                            meta:{isAuth:true},
                        }
                    ]
                },
            ]
        }
    ]
});

router.beforeEach((to,from,next) => {
    if (to.meta.isAuth) {
        if (localStorage.getItem("username") != null && localStorage.getItem("password") != null) {
            next();
        } else {
            router.push('/');
            alert('请先登入在操作');
        }
    } else {
        next();
    }
});

export default router;